package com.youdo.careyes.ui;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.youdo.careyes.R;
import com.youdo.careyes.databinding.FragmentSetBinding;
import com.youdo.careyes.utils.AppUtil;
import com.youdo.careyes.utils.Constants;
import com.youdo.careyes.utils.SpUtil;
import com.youdo.careyes.utils.ToastUtil;
import com.youdo.careyes.widget.BxSuperSetView;
import com.youdo.helper.UVCCameraHelper;

/**
 * 设置页面
 *
 * @author ljb
 * @date 2025.07.30
 * @email 568838953@qq.com
 */
public class SetFragment extends Fragment {
    private final SpUtil sp = SpUtil.getInstance();
    private final UVCCameraHelper mCameraHelper = UVCCameraHelper.getInstance();
    private FragmentSetBinding binding;

    Handler handler = new Handler();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentSetBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.swScreen.setChecked(sp.getBoolean(Constants.ALLOW_SCREEN_OFF));
        binding.swSound.setChecked(sp.getBoolean(Constants.VIDEO_SOUND_SW));
        binding.swHorizontal.setChecked(sp.getBoolean(Constants.CAMERA_HORIZONTAL));
        binding.swVertical.setChecked(sp.getBoolean(Constants.CAMERA_VERTICAL));
        int pos1 = AppUtil.constrain(sp.getInt(Constants.SELECT_CACHE_PATH), 0, 1);
        binding.spinnerSelectCachePath.setSelection(pos1)
                .setTip(sp.getSavePath())
                .setSsClickListener(new BxSuperSetView.OnSsClick() {
                    @Override
                    public void onSsLeftClick(int id, BxSuperSetView ss, int spinnerPos) {
                        binding.spinnerSelectCachePath.setTip(sp.getSavePath());
                    }
                });
        int pos2 = AppUtil.constrain(sp.getInt(Constants.VIDEO_RESOLUTION), 0, 2);
        binding.spinnerVideoResolution.setSelection(pos2);
        binding.buttonSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp.put(Constants.ALLOW_SCREEN_OFF, binding.swScreen.isChecked());
                AppUtil.keepScreenOn(getActivity(), !sp.getBoolean(Constants.ALLOW_SCREEN_OFF));
                sp.put(Constants.VIDEO_SOUND_SW, binding.swSound.isChecked());
                int position1 = binding.spinnerSelectCachePath.getSelectedItemPosition();
                if (position1 != pos1) {
                    sp.put(Constants.SELECT_CACHE_PATH, position1);
                    mCameraHelper.setSaveRootPath(sp.getSavePath());// 设置保存路径
                }
                int position2 = binding.spinnerVideoResolution.getSelectedItemPosition();
                if (position2 != pos2) {
                    sp.put(Constants.VIDEO_RESOLUTION, position2);
                    int[] arr = sp.getResolution();
                    mCameraHelper.resize(arr[0], arr[1]);
                }
                NavHostFragment.findNavController(SetFragment.this).navigateUp();
                ToastUtil.showLong(R.string.app_public_save_success_and_restart);
                if (position1 != pos1) {
                    UVCCameraHelper.getInstance().setSaveRootPath(SpUtil.getInstance().getSavePath());
                    if (UVCCameraHelper.getInstance().isRecording()) {
                        UVCCameraHelper.getInstance().recordVideo(false);
                        handler.removeCallbacks(recordRun);
                        handler.postDelayed(recordRun, 3000);
                    }
                }
            }
        });
        binding.swHorizontal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean check = binding.swHorizontal.isChecked();
                binding.swHorizontal.setChecked(!check);
                UVCCameraHelper.getInstance().setOrientation(!check ? 1 : 0, 0);
                sp.put(Constants.CAMERA_HORIZONTAL, !check);
            }
        });
        binding.swVertical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean check = binding.swVertical.isChecked();
                binding.swVertical.setChecked(!check);
                UVCCameraHelper.getInstance().setOrientation(!check ? 1 : 0, 1);
                sp.put(Constants.CAMERA_VERTICAL, !check);
            }
        });
    }

    private Runnable recordRun = new Runnable() {
        @Override
        public void run() {
            UVCCameraHelper.getInstance().recordVideo(true);
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}